﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Glyphica
{
    class GameSession
    {

        //
        // Class-level variables
        //
        Random rand;                        // Session-level randomizer that will drive most random number generation
        MapLevel[] MapLevels;               // The array that holds all of the individual MapLevel objects
        int CurrentMapLevel;                // The array index of the currently-active MapLevel
        MapPoint PlayerMapPosition;         // Where the player is on the map
        MapPoint PlayerScreenPosition;      // Where the player is on the screen
        int MapViewportWidth = 70;          // The width of the map viewport
        int MapViewportHeight = 30;         // The height of the map viewport
                
        public void StartGame()
        {
            Console.WindowWidth = 100;
            Console.WindowHeight = 50;
            Console.Clear();

            // Create the array that will hold all map levels
            MapLevels = new MapLevel[66];
            CurrentMapLevel = 0;
            rand = new Random(123);

            // Generate the first level of the map
            MapBuilder mb = new MapBuilder(rand);
            MapLevels[CurrentMapLevel] = mb.GenerateMap();

            // Put the player in the center of the map viewport
            PlayerScreenPosition = new MapPoint((int)MapViewportHeight / 2, (int)MapViewportWidth / 2);
            PlayerMapPosition = MapLevels[CurrentMapLevel].StairsDown;

            // Draw the screen
            DrawMap();
            DrawPlayer();
        }

        void DrawMap() 
        {

            MapLevel ThisLevel = MapLevels[CurrentMapLevel];

            // Calculate which part of the map is to be displayed in the viewport
            int top = PlayerMapPosition.Row - PlayerScreenPosition.Row;
            top = (top>0) ? top : 0;

            int left = PlayerMapPosition.Column - PlayerScreenPosition.Column;
            left = (left > 0) ? left : 0;

            int bottom = top + MapViewportHeight;
            bottom = (bottom < ThisLevel.Height) ? bottom : ThisLevel.Height - 1;

            int right = left + MapViewportWidth;
            right=(right < ThisLevel.Width) ? right : ThisLevel.Width-1;
            
            // Draw the appropriate piece of the map
            Console.CursorTop = 0;
            Console.CursorLeft = 0;
            for (int row = top; row <= bottom; row++)
            {
                for (int column = left; column <= right; column++)
                {
                    DrawTile(ThisLevel.Tiles[row,column]);
                }
                Console.WriteLine();
            }
        }

        void DrawPlayer()
        {
            Console.CursorTop = PlayerScreenPosition.Row;
            Console.CursorLeft = PlayerScreenPosition.Column;
            Console.Write("@");
        }
        
        void DrawCharacterWindow() { }

        private void DrawTile(MapTile tile)
        {
            string TileCharacter;
            if (tile.IsVisible)
            {
                switch (tile.Type)
                {
                    case MapTile.TileType.Empty: TileCharacter = "."; break;
                    case MapTile.TileType.Stone: TileCharacter = "#"; break;
                    case MapTile.TileType.Door_Open: TileCharacter = "/"; break;
                    default: TileCharacter = " "; break;
                }
            }
            else
            {
                TileCharacter = " ";
            }

            Console.Write(TileCharacter);
        }

        public void PlayGame()
        {
            do
            {
                ConsoleKeyInfo k = Console.ReadKey(true);
                switch (k.Key)
                {
                }
            }
            while (1 == 1);

        }
        
    }
}
